// pages/make-detial/make-detial.js
const moment = require("../../utils/moment.min.js")

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['全部','体适能培训', '花式跳绳','游泳', '羽毛球','冬夏令营','中考'],
    index: 0,
    img: '/images/coach.png',
    dateList:[],
    loading:false
  },
  // 选择器的方法
  bindPickerChange: function (e) {
    var me = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var index = e.detail.value;
    var dateList = [];
    if(index == 0 ) {
      me.onLoad();
    }else{
      wx.getStorage({
        key: 'childcode',
        success: function (res) {
          var childcode = res.data;
          console.log(childcode)
          wx.request({
            url: app.serverUrl + '/getSignalClassRecords',
            method: "post",
            data: {
              childCode: childcode,
              classCode: index
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            success: function (res) {
              if (res.data.status == 200) {
                var item = res.data.data;
                console.log(item)
                var array = [];
                if(item.length !== 0){
                  for (var i = 0; i < item.length; i++) {
                    var news_item = item[i].signtime;
                    var time = moment(news_item).format('YYYY-MM-DD HH:mm:ss');
                    array = [{
                      signtime: time,
                      childcode: item[i].childcode,
                      classname: item[i].classname
                    }]
                    dateList = dateList.concat(array);
                  }
                } else {
                  wx.showToast({
                    title: '无签到信息',
                    icon: 'success',
                    duration: 2000,
                    mask: true,
                    success: function (res) {
                      setTimeout(function () {
                        me.onLoad(); 
                      }, 2000)
                    }
                  })
                }
              }
              me.setData({
                dateList: dateList
              })
            }
          })
        }
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var me = this;
    var dateList = me.data.dateList;
    if (this.data.loading) {
      return;
    }
    wx.showLoading({
      title: '数据加载中',
    })
    this.setData({
      loading: true
    })
    wx.getStorage({
      key: 'childcode',
      success: function (res) {
        var childcode = res.data;
        wx.request({
          url: app.serverUrl + '/getSignRecords',
          method: "post",
          data: {
            childCode: childcode
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (res) {
            if (res.data.status == 200) {
              var item = res.data.data;
              var array = [];
              for (var i = 0; i < item.length; i++) {
                var news_item = item[i].signtime;
                var time = moment(news_item).format('YYYY-MM-DD HH:mm:ss');
                array = [{
                  signtime: time,
                  childcode: item[i].childcode,
                  classname: item[i].classname
                }]
                dateList = dateList.concat(array);
              }
            }
            me.setData({
              dateList: dateList
            })

          },
          
          complete: () => {
            wx.hideLoading()
            me.setData({
              loading: false
            })
          }
        })


      },
      fail: function () {
       
       
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})